package com.vecspace.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.Properties;

import com.vecspace.utils.log.HLogger;

public class AppPropUtil {
	/**主线程是否已经退出*/
	public static volatile boolean appExited = false;
	
	/**获取进程ID
	 * @return 
	 * @author wanghua
	 */
	public static int getProcessId(){
		String name = ManagementFactory.getRuntimeMXBean().getName();    
		System.out.println(name);
		String pIdStr = name.split("@")[0];  
		int pId = Integer.parseInt(pIdStr);
		HLogger.defaultLogger().info("PID："+pId+","+name);
		return pId;
	}
	
	public static String getProperty(String propName){
		try {
			Properties props = loadProperties();
			if(props != null){
				String val = props.getProperty(propName);
				return val;
			}else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	public static Properties loadProperties() throws Exception{
		String propertiesFileName = "application.properties";
		return loadProperties(propertiesFileName);
	}
	
	public static Properties loadProperties(String propertiesFileName) throws Exception{
		Properties props = new Properties();
		File file = new File(propertiesFileName);				
		InputStream fis=null;
		InputStreamReader reader = null;
		if(!file.exists()){ 
			fis = AppPropUtil.class.getResourceAsStream("/"+propertiesFileName);
			if(fis == null){
				System.out.println("没有找到文件："+propertiesFileName);
				return null;
			}
		}else{
			fis = new FileInputStream(file);
		}
		try{
			reader = new InputStreamReader(fis,"UTF-8"); 
			props.load(reader);
		}finally{
			if(reader != null)reader.close();
		}
		return props;
	}
}
